package com.njg.juc.pool;

import java.util.concurrent.*;

/**
 * @auther zzyybs@126.com
 * @Date 2025-04-24 15:49
 * @Description: TODO
 */
public class ThreadPoolDemo
{
    public static void main(String[] args)
    {
        //自定义线程池
        ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(Runtime.getRuntime().availableProcessors(),
                Runtime.getRuntime().availableProcessors()+1,1L,TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(3),Executors.defaultThreadFactory(),new ThreadPoolExecutor.CallerRunsPolicy());

        for (int i = 0; i < 50; i++) {
            int finalI = i;
            poolExecutor.execute(()->{
                System.out.println(Thread.currentThread().getName() + "\t" + "为:"+finalI+"办理业务");
            });
        }

//        defaultPool();
    }

    private static void defaultPool()
    {
//        ExecutorService threadPool = Executors.newFixedThreadPool(5);//一池固定数
//        ExecutorService threadPool = Executors.newSingleThreadExecutor();//一池1线程
        ExecutorService threadPool = Executors.newCachedThreadPool();//一池N线程

       try {
           for (int i = 1; i <= 10; i++) {
               int finalI = i;
               threadPool.execute(()->{
                   System.out.println(Thread.currentThread().getName() + "\t" + "为:"+finalI+"办理业务");
               });
           }
       }catch (Exception e){
           e.printStackTrace();
       }finally {
           threadPool.shutdown();
       }
    }
}
